/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.io.Serializable;
import java.util.*;
import dto.*; 
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import util.ConnectToMySql;
 
public class NguoiDungDAO implements Serializable {
    public static List<NguoiDung> LayDanhSachNguoiDung() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
         List<NguoiDung> list = new ArrayList<NguoiDung>();
        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from NguoiDung ");
                  ResultSet rs = cs.executeQuery();
                if (rs != null) {
                     while (rs.next()) {
                     NguoiDung my = new NguoiDung();
                    my.setTendangnhap(rs.getString("tendangnhap"));
                    my.setMatkhau(rs.getString("matkhau"));
                    my.setHoten(rs.getString("hoten"));
                    my.setEmail(rs.getString("email"));
                    my.setDienThoai(rs.getString("dienthoai"));
                    my.setGioiTinh(rs.getInt("gioitinh"));  
                     my.setNgaysinh(rs.getString("ngaysinh"));
                    my.setTrangthai(rs.getInt("trangthai")); 
                     my.setMaloainguoidung(rs.getInt("maloainguoidung"));  
                     //  LoaiNguoiDung lnd = LoaiNguoiDungDAO.LayThongTinLoaiNguoiDung(rs.getInt("maloainguoidung"));
                  // my.setLoaiNguoiDung(lnd);
                     list.add(my);
                }
                }
            } catch (Exception ex) {
                System.out.println(ex);
                return null;
            }
        }
        
        return list;
    }
     public static NguoiDung LayThongTinNguoiDung(String tenDangNhap) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
        NguoiDung nd = new NguoiDung();

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from NguoiDung where tendangnhap=?");
                cs.setString(1, tenDangNhap); 
                ResultSet rs = cs.executeQuery();
                if (rs != null) {
                    if (rs.next() == true) {
                        NguoiDung my = new NguoiDung();
                    my.setTendangnhap(rs.getString("tendangnhap"));
                    my.setMatkhau(rs.getString("matkhau"));
                    my.setHoten(rs.getString("hoten"));
                    my.setEmail(rs.getString("email"));
                    my.setDienThoai(rs.getString("dienthoai"));
                    my.setGioiTinh(rs.getInt("gioitinh"));  
                     my.setNgaysinh(rs.getString("ngaysinh"));
                    my.setTrangthai(rs.getInt("trangthai"));
                     my.setMaloainguoidung(rs.getInt("maloainguoidung"));  
                     nd= my;
                    } else {
                        nd = null;
                    }
                }
            } catch (Exception ex) {
                return null;
            }
        }
        return nd;
    }
     public static int SoLuongNguoiDung() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
        int kq = 0;
       List<NguoiDung> list = null;
        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from NguoiDung");
                  ResultSet rs = cs.executeQuery();
                if (rs != null) {
                     while (rs.next()) {
                     NguoiDung my = new NguoiDung();
                    my.setTendangnhap(rs.getString("tendangnhap"));
                    my.setMatkhau(rs.getString("matkhau"));
                    my.setHoten(rs.getString("hoten"));
                    my.setEmail(rs.getString("email"));
                    my.setDienThoai(rs.getString("dienthoai"));
                    my.setGioiTinh(rs.getInt("gioitinh"));  
                     my.setNgaysinh(rs.getString("ngaysinh"));
                    my.setTrangthai(rs.getInt("trangthai"));
                     my.setMaloainguoidung(rs.getInt("maloainguoidung")); 
                     list.add(my);
                }
                }
            } catch (Exception ex) {
                System.out.println(ex); 
            }
        }
        kq= list.size();
        return kq;
    }
     
    public static boolean ThemNguoiDung(NguoiDung nd) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
       boolean result = false;

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("insert into NguoiDung values(?,?,?,?,?,?,?,?,?)");
            cs.setString(1, nd.getTendangnhap());
            cs.setString(2, nd.getMatkhau());
            cs.setString(3, nd.getHoten());
            cs.setString(4, nd.getEmail());
            cs.setString(5, nd.getDienThoai());
            cs.setInt(6, nd.getGioiTinh());
            cs.setString(7, nd.getNgaysinh());
            cs.setInt(8, nd.getTrangthai());
            cs.setInt(9, nd.getMaloainguoidung());

            int n = 0;
            try {
                n = cs.executeUpdate();
                if (n > 0) {
                    result = true;
                }
            } catch (Exception ex) {
                result = false;
            }
        }

        return result;
    }
    
    public static boolean CapNhatNguoiDung(NguoiDung nd)throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
     boolean result = false;

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("Update nguoidung Set MatKhau = ?, hoten=?,email=?,dienthoai=?,gioitinh=?,ngaysinh=?,trangthai=?,maloainguoidung=?   Where TenDangNhap = ?");
         
            cs.setString(1, nd.getMatkhau());
            cs.setString(2, nd.getHoten());
            cs.setString(3, nd.getEmail());
            cs.setString(4, nd.getDienThoai());
            cs.setInt(5, nd.getGioiTinh());
            cs.setString(6,   nd.getNgaysinh());
            cs.setInt(7, nd.getTrangthai());
            cs.setInt(8, nd.getMaloainguoidung());
             cs.setString(9, nd.getTendangnhap());

            int n = 0;
            try {
                n = cs.executeUpdate();
                if (n > 0) {
                    result = true;
                }
            } catch (Exception ex) {
                result = false;
            }
        }

        return result;  
    }
    
    public static boolean XoaNguoiDung(String tenDangNhap)throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
       boolean result = false;

        Connection conn = ConnectToMySql.getConnection();
        if (conn != null) {
            CallableStatement cs = conn.prepareCall("delete from nguoidung where tendangnhap=?");
            cs.setString(1, tenDangNhap);

            int n = 0;
            try {
                n = cs.executeUpdate();
                if (n > 0) {
                    result = true;
                }
            } catch (Exception ex) {
                result = false;
            }
        }

        return result;
    }
    
     
    public static boolean checkLogin(String username, String password) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
       boolean kq = false;
         Connection conn= ConnectToMySql.getConnection();
         if (conn != null) {
            try {
                CallableStatement cs = conn.prepareCall("select * from NguoiDung where tendangnhap=? and matkhau=?" );
                cs.setString(1, username);
               cs.setString(2, password);
                  ResultSet rs = cs.executeQuery();
                if (rs != null) {
                     while (rs.next()) {
                     NguoiDung my = new NguoiDung();
                    my.setTendangnhap(rs.getString("tendangnhap"));
                    my.setMatkhau(rs.getString("matkhau"));
                    my.setHoten(rs.getString("hoten"));
                    my.setEmail(rs.getString("email"));
                    my.setDienThoai(rs.getString("dienthoai"));
                    my.setGioiTinh(rs.getInt("gioitinh"));  
                     my.setNgaysinh(rs.getString("ngaysinh"));
                    my.setTrangthai(rs.getInt("trangthai"));
                     my.setMaloainguoidung(rs.getInt("maloainguoidung"));  
                     kq= true;
                }
                }
            } catch (Exception ex) {
                System.out.println(ex);
                kq=  false;
            }
        }
        return kq;
    }
}
